package mqtt;

import HslCommunication.Core.Types.ActionOperateExTwo;
import HslCommunication.Core.Types.OperateResultExTwo;
import HslCommunication.MQTT.MqttSyncClient;
import org.junit.jupiter.api.Assertions;

import java.text.SimpleDateFormat;
import java.util.Date;

public class MqttSyncClientTest {
    @org.junit.jupiter.api.Test
    public void MqttSyncClientUnitTest(){
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        MqttSyncClient mqttSync = new MqttSyncClient( "127.0.0.1", 1883 );
        mqttSync.SetPersistentConnection( );

        for (int i = 0; i < 100; i++)
        {
            OperateResultExTwo<String, String> readA = mqttSync.ReadString( "A", "测试的数据A", null, null, null );
            if (readA.IsSuccess)
            {
                System.out.println( formatter.format(new Date()) + " 数据A接收成功!" + readA.Content1 + ":" + readA.Content2 );
            }
            else
            {
                System.out.println( formatter.format(new Date()) + " 数据A接收失败!" + readA.Message );
            }

            Assertions.assertEquals("B", readA.Content1);
            Assertions.assertEquals("这是回传的一条测试数据", readA.Content2);
        }

        for (int i = 0; i < 3; i++)
        {
            OperateResultExTwo<String, String> readB = mqttSync.ReadString( "B", "测试的数据",
                    new ActionOperateExTwo<Long, Long>(){
                        @Override
                        public void Action(Long m, Long n) {
                            System.out.println( formatter.format(new Date()) + " 已发送：" + m + "/" + n);
                        }
                    },
                    new ActionOperateExTwo<String, String>(){
                        @Override
                        public void Action(String m, String n) {
                            System.out.println(formatter.format(new Date()) + " 服务器正在处理:" + m + ", 进度：" + n);
                        }
                    },
                    new ActionOperateExTwo<Long, Long>(){
                        @Override
                        public void Action(Long m, Long n) {
                            System.out.println(formatter.format(new Date()) + " 已接收：" + m + "/" + n);
                        }
                    });
            if (readB.IsSuccess)
            {
                System.out.println( formatter.format(new Date()) + " 数据B接收成功!" + readB.Content1 + ":" + readB.Content2 );
            }
            else
            {
                System.out.println( formatter.format(new Date()) + " 数据B接收失败!" + readB.Message );
            }
        }

        OperateResultExTwo<String, String> readC = mqttSync.ReadString( "C", "测试的数据",
                new ActionOperateExTwo<Long, Long>(){
                    @Override
                    public void Action(Long m, Long n) {
                        System.out.println( formatter.format(new Date()) + " 已发送：" + m + "/" + n);
                    }
                },
                new ActionOperateExTwo<String, String>(){
                    @Override
                    public void Action(String m, String n) {
                        System.out.println(formatter.format(new Date()) + " 服务器正在处理:" + m + ", 进度：" + n);
                    }
                },
                new ActionOperateExTwo<Long, Long>(){
                    @Override
                    public void Action(Long m, Long n) {
                        System.out.println(formatter.format(new Date()) + " 已接收：" + m + "/" + n);
                    }
                });
        if (readC.IsSuccess)
        {
            System.out.println( formatter.format(new Date()) + " 数据C接收成功!" + readC.Content1 );
        }
        else
        {
            System.out.println( formatter.format(new Date()) + " 数据C接收失败!" + readC.Message );
        }


        mqttSync.ConnectClose();

    }
}
